/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mrp.persistencia.controller;

import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import javax.transaction.UserTransaction;
import mrp.beans.util.EntityManagerUtil;
import mrp.persistencia.controller.exceptions.NonexistentEntityException;
import mrp.persistencia.controller.exceptions.PreexistingEntityException;
import mrp.persistencia.controller.exceptions.RollbackFailureException;
import mrp.persistencia.entity.Estructuramaterialesbom;
import mrp.persistencia.entity.Modelocalzado;

/**
 *
 * @author root
 */
public class EstructuramaterialesbomJpaController implements Serializable {

    public EstructuramaterialesbomJpaController() {
        
        this.emf = EntityManagerUtil.getEntityManagerFactory();
    }

    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    public void create(Estructuramaterialesbom estructuramaterialesbom) throws PreexistingEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
             
            em = getEntityManager();
            em.getTransaction().begin();
            Modelocalzado modelocalzadoFk = estructuramaterialesbom.getModelocalzadoFk();
            if (modelocalzadoFk != null) {
                modelocalzadoFk = em.getReference(modelocalzadoFk.getClass(), modelocalzadoFk.getCodestructuramateria());
                estructuramaterialesbom.setModelocalzadoFk(modelocalzadoFk);
            }
            em.persist(estructuramaterialesbom);
            if (modelocalzadoFk != null) {
                modelocalzadoFk.getEstructuramaterialesbomList().add(estructuramaterialesbom);
                modelocalzadoFk = em.merge(modelocalzadoFk);
            }
             em.getTransaction().commit();
        } catch (Exception ex) {
            try {
                 em.getTransaction().rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            if (findEstructuramaterialesbom(estructuramaterialesbom.getCodbom()) != null) {
                throw new PreexistingEntityException("Estructuramaterialesbom " + estructuramaterialesbom + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Estructuramaterialesbom estructuramaterialesbom) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
             
            em = getEntityManager();
            em.getTransaction().begin();
            Estructuramaterialesbom persistentEstructuramaterialesbom = em.find(Estructuramaterialesbom.class, estructuramaterialesbom.getCodbom());
            Modelocalzado modelocalzadoFkOld = persistentEstructuramaterialesbom.getModelocalzadoFk();
            Modelocalzado modelocalzadoFkNew = estructuramaterialesbom.getModelocalzadoFk();
            if (modelocalzadoFkNew != null) {
                modelocalzadoFkNew = em.getReference(modelocalzadoFkNew.getClass(), modelocalzadoFkNew.getCodestructuramateria());
                estructuramaterialesbom.setModelocalzadoFk(modelocalzadoFkNew);
            }
            estructuramaterialesbom = em.merge(estructuramaterialesbom);
            if (modelocalzadoFkOld != null && !modelocalzadoFkOld.equals(modelocalzadoFkNew)) {
                modelocalzadoFkOld.getEstructuramaterialesbomList().remove(estructuramaterialesbom);
                modelocalzadoFkOld = em.merge(modelocalzadoFkOld);
            }
            if (modelocalzadoFkNew != null && !modelocalzadoFkNew.equals(modelocalzadoFkOld)) {
                modelocalzadoFkNew.getEstructuramaterialesbomList().add(estructuramaterialesbom);
                modelocalzadoFkNew = em.merge(modelocalzadoFkNew);
            }
             em.getTransaction().commit();
        } catch (Exception ex) {
            try {
                 em.getTransaction().rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                String id = estructuramaterialesbom.getCodbom();
                if (findEstructuramaterialesbom(id) == null) {
                    throw new NonexistentEntityException("The estructuramaterialesbom with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(String id) throws NonexistentEntityException, RollbackFailureException, Exception {
        EntityManager em = null;
        try {
             
            em = getEntityManager();
            em.getTransaction().begin();
            Estructuramaterialesbom estructuramaterialesbom;
            try {
                estructuramaterialesbom = em.getReference(Estructuramaterialesbom.class, id);
                estructuramaterialesbom.getCodbom();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The estructuramaterialesbom with id " + id + " no longer exists.", enfe);
            }
            Modelocalzado modelocalzadoFk = estructuramaterialesbom.getModelocalzadoFk();
            if (modelocalzadoFk != null) {
                modelocalzadoFk.getEstructuramaterialesbomList().remove(estructuramaterialesbom);
                modelocalzadoFk = em.merge(modelocalzadoFk);
            }
            em.remove(estructuramaterialesbom);
            em.getTransaction().commit();
        } catch (Exception ex) {
            try {
                 em.getTransaction().rollback();
            } catch (Exception re) {
                throw new RollbackFailureException("An error occurred attempting to roll back the transaction.", re);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Estructuramaterialesbom> findEstructuramaterialesbomEntities() {
        return findEstructuramaterialesbomEntities(true, -1, -1);
    }

    public List<Estructuramaterialesbom> findEstructuramaterialesbomEntities(int maxResults, int firstResult) {
        return findEstructuramaterialesbomEntities(false, maxResults, firstResult);
    }

    private List<Estructuramaterialesbom> findEstructuramaterialesbomEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Estructuramaterialesbom.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Estructuramaterialesbom findEstructuramaterialesbom(String id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Estructuramaterialesbom.class, id);
        } finally {
            em.close();
        }
    }

    public int getEstructuramaterialesbomCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Estructuramaterialesbom> rt = cq.from(Estructuramaterialesbom.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    public List<Estructuramaterialesbom> findEstructuramaterialesbomWhereOrderBy(String consulta,boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
//            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
//            cq.select(cq.from(Cliente.class));
//            Query q = em.createQuery(cq);
//            Query q = em.createQuery("SELECT o FROM Cliente o where "+ +" order by o.numerocliente");
            Query q = em.createQuery(consulta);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }
    public int getEstructuramaterialesbomCountWhere(String codestructuramateria) {
        EntityManager em = getEntityManager();
        try {
//            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
//            Root<Estructuramaterialesbom> rt = cq.from(Estructuramaterialesbom.class);
//            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery("select count(mc.codbom) from Estructuramaterialesbom as mc where mc.modelocalzadoFk.codestructuramateria= '"+codestructuramateria+"'");
//            Query query=em.createQuery("SELECT COUNT
//(emp.empName) FROM Employee emp");
//            List<Estructuramaterialesbom> listacount= this.findEstructuramaterialesbomWhereOrderBy("select mc from Estructuramaterialesbom as mc where mc.modelocalzadoFk.codestructuramateria= "+codestructuramateria, true, -1, -1);                    
            return ((Number) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
